package com.atguigu.spzx.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.common.contant.RedisConstant;
import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.common.utils.MyAssert;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public LoginVo login(LoginDto loginDto) {

        String captcha = loginDto.getCaptcha();
        String codeKey = loginDto.getCodeKey();
        MyAssert.isTrue(!StringUtils.isEmpty(captcha) && ! StringUtils.isEmpty(codeKey), "验证码不通过");

        // 校验验证码
        String redisKey = RedisConstant.USER_LOGIN_VALIDATECODE + codeKey;
        String captchaFromRedis = redisTemplate.opsForValue().get(redisKey);
        MyAssert.notNull(captchaFromRedis, "验证码Key错误");
        MyAssert.isTrue(captchaFromRedis.equalsIgnoreCase(captcha), "验证码不正确");

        redisTemplate.delete(redisKey);

        String userName = loginDto.getUserName();
        String password = loginDto.getPassword();

        SysUser sysUser = sysUserMapper.selectSysUserByName(userName);

        MyAssert.notNull(sysUser, "没有该用户");

        String passwordFromDB = sysUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        MyAssert.isTrue(!password.equals(passwordFromDB),"密码错误");

        LoginVo loginVo = new LoginVo();
        String token = UUID.randomUUID().toString().replace("-", "");
        loginVo.setToken(token);

        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_PREFIX + token, JSON.toJSONString(sysUser));



        return loginVo;
    }

    @Override
    public SysUser getUserInfo(String token) {
        String sysUserJson = redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_PREFIX + token);
        MyAssert.notNull(sysUserJson, "认证失败");
        SysUser sysUser = JSON.parseObject(sysUserJson, SysUser.class);
        return sysUser;
    }

    @Override
    public void logout(String token) {
        redisTemplate.delete("user:login:" + token);
    }

    @Override
    public PageInfo<SysUser> findByPage(SysUserDto sysUserDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUserList = sysUserMapper.findByPage(sysUserDto);
        return new PageInfo<>(sysUserList);
    }

    @Override
    public void savaSysUser(SysUser sysUser) {
        // 看看用户名是否已存在
        SysUser sysUserFromDB = sysUserMapper.selectSysUserByName(sysUser.getUserName());
        MyAssert.isNull(sysUserFromDB,"用户名已存在");

        // 加密密码
        String password = sysUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        sysUser.setPassword(password);
        sysUserMapper.insertSysUser(sysUser);
    }

    @Override
    public void updateSysUser(SysUser sysUser) {
        sysUserMapper.updateSysUser(sysUser);
    }

    @Override
    public void deleteById(Long userId) {
        sysUserMapper.deleteById(userId);
    }
}
